package cn.gjxblog.changyan;

import cn.gjxblog.api.UserServiceFegin;
import cn.gjxblog.model.CommonConstants;
import cn.gjxblog.model.UserContext;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/changyan")
public class LoginController {
    public static final String TOKEN_COOKIE = "access_token";
    @Autowired
    private UserServiceFegin userServerFegin;
    //该接口只有当畅言已登录，getUserInfo返回未登录时，才会被调用用来登录自身网站
    @RequestMapping("login")
    public void loginByCy(@RequestParam(value = "callback") String callback,
                          @RequestParam(value = "user_id") String user_id,
                          @RequestParam(value = "nickname") String nickname,
                          @RequestParam(value = "sign") String sign, @RequestParam(value = "img_url") String img_url,
                          @RequestParam(value = "profile_url") String profile_url, HttpServletResponse resp) throws IOException {
        //自己网站的登录逻辑，记录登录信息到cookie
        Cookie cookie = new Cookie("user_id", user_id);

        resp.addCookie(cookie);
        resp.getWriter().write(callback + "({\"user_id\":"+user_id+",reload_page:0})");
    }

    @RequestMapping("loginout")
    public void loginBySite(@RequestParam(value = "callback") String callback,HttpServletResponse resp) throws IOException {
        //清除自己网站cookies信息,同时前端logout.js代码用来清理畅言cookie
        resp.getWriter().write(callback + "({\"code\":\"1\",reload_page:0, js-src:logout.js})");
    }


    @RequestMapping("getUserInfo")
    public void getUserInfo(@RequestParam(value = "callback") String callback, HttpServletRequest res,
                            HttpServletResponse resp,HttpServletRequest req) throws IOException {
        UserInfo userinfo = new UserInfo();
        Cookie cookie = WebUtils.getCookie(req, TOKEN_COOKIE);
        if (cookie != null && StringUtils.isNoneBlank(cookie.getValue())) {
            cn.gjxblog.model.entity.User myuser = userServerFegin.getUserByToken(cookie.getValue()).getResult();
            userinfo.setIs_login(1);
            User user = new User();
            user.setUser_id(1);
            user.setNickname(myuser.getName());
            user.setImg_url(myuser.getAvatar());
            user.setProfile_url("/accounts/profile");
            user.setSign(myuser.getName());
            userinfo.setUser(user);
        }else{
            userinfo.setIs_login(0);//用户未登录

        }
        resp.setContentType("application/x-javascript; charset=UTF-8");
        resp.getWriter().write(callback+"("+ JSONArray.toJSONString(userinfo)+")");
    }

    //该方法判断cookie中是否存在键值为key的value值
    public boolean isContains(String key, Cookie[] cookies){
        for(Cookie cookie : cookies){
            if(cookie.getName().equals(key)){
                if(cookie.getValue()!=null){
                    return true;
                }else{
                    return false;
                }
            }
        }
        return false;
    }

    //该方法获取cookie中键值为key的value值
    public Object getCookValue(String key,Cookie[] cookies){
        for(Cookie cookie :cookies){
            if(cookie.getName().equals(key))
                return cookie.getValue();
        }
        return null;
    }
}
